package com.fyzk.activity.lottery.jc.football;


import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanDialog;
import com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanInfor;
import com.fyzk.activity.lottery.zc.tz_dialog.JZ_HG_Pop;
import com.fyzk.activity.zhifu.SportsFootballPayActivity;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.LogUtils;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.utils.ScreenUtils;
import com.fyzk.utils.Utils;

import java.util.List;

import static com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanDialog.result;

public class JCZQ_HG_ZhiFuActivity extends SportsFootballPayActivity {

    private static final String TAG = "JC_HHZhiFuActivity";

    @Override
    protected void onResume() {
        super.onResume();
        buyType.setText("竞彩足球-混合投注");
    }

    @Override
    protected void clear() {
        // TODO Auto-generated method stub
        super.clear();
        adapter.setInfors(infos, isDanguan);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void initAdapter() {
        adapter = new JCZQ_HG_ZhiFuAdapter(this, isDanguan, infos, handler, this);
        listView.setAdapter(adapter);
    }

    /**
     * 获取投注内容
     *
     * @param infos
     */
    public String getContent(List<JCInfo> infos, boolean isdanguan) {
        List<String> chuans = JC_GuoGuanInfor.getChuanIds(result, isdanguan);
        String content = "";
        for (int i = 0; i < chuans.size(); i++) {
            content += lotteryId + "" + wanFa + chuans.get(i) + "-";//301012001-
            String n = "";
            String danCodes = "";
            for (int j = 0; j < infos.size(); j++) {
                JCInfo info = infos.get(j);
                String danChang = "";
                if (info.getOnclikNum() > 0) {
                    danChang += JC_GuoGuanInfor.getHunHes(info);
                }

                if (!danChang.equals("")) {
                    if (info.isDan()) {
                        danCodes += danChang + "|";
                    } else {
                        n += danChang + "|";
                    }
                }
            }

            if (!danCodes.equals("")) {
                danCodes = danCodes.substring(0, danCodes.length() - 1);
                danCodes += "#";
            }
            if (!n.equals("")) {
                n = n.substring(0, n.length() - 1);
                n += "^";
            }
            content += danCodes + n + "!";
        }
        if (!content.equals("")) {
            content = content.substring(0, content.length() - 1);
        }
        Log.e(TAG, "content  " + content);
        return content;
    }

    @Override
    protected int getMaxGuoGuan() {
        // TODO Auto-generated method stub
        int max_guan = 8;
        String content = getContent();
        if (content.contains(LotteryIDUtils.JZBF_LOTTERY_ID_INT + "(") || content.contains
                (LotteryIDUtils.JZBQC_LOTTERY_ID_INT + "(")) {
            max_guan = 4;
        } else if (content.contains(LotteryIDUtils.JZZJQ_LOTTERY_ID_INT + "(")) {
            max_guan = 6;
        }
        return max_guan;
    }

    int tempMaxGuan = 8;

    @Override
    public void open(final JCInfo info) {
        int height = Utils.dip2px(440f);
        JZ_HG_Pop JZHGPop = new JZ_HG_Pop(getContext(), WindowManager.LayoutParams
                .MATCH_PARENT, height, info, changCi);
        tempMaxGuan = getMaxGuoGuan();
        JZHGPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1.0f);
                adapter.notifyDataSetChanged();
                LogUtil.e("info.getOnclikNum()"+info.getOnclikNum());
                if (info.getOnclikNum() == 0) {
                    onDelete(info);
                } else {
                    if (!result.equals(JC_GuoGuanDialog.GG_RESULT) && getMaxSelGuan() > getMaxGuoGuan()) {
                        result = JC_GuoGuanDialog.GG_RESULT;
                        tv_guoGuanStyle.setText(result);
                    }
                }
            }

        });
        setBackgroundAlpha(0.5f);
        JZHGPop.showAtLocation(getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, ScreenUtils
                .getInstance()
                .getHeight() -
                height);
    }

    /**
     * 获取选择的最大过关数
     *
     * @return
     */
    public int getMaxSelGuan() {
        String[] chuans = JC_GuoGuanDialog.result.split(",");
        return Integer.parseInt(chuans[chuans.length - 1].substring(0, 1));
    }

    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }
}
